﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using TRPOP.CMS.Template.GenerateCS;
using TRPOP.CMS.Plugin;
using System.IO;
using System.Reflection;
using TRPOP.Cache;

namespace TRPOP.CMS.Template
{
    public class ExecuteTemplate
    {
        public static ICache mICache = ICacheAccess.GetInstance();

        /// <summary>
        ///  根据指定的dll文件，执行模板解析
        /// </summary>
        /// <param name="oTemplateDllPageInfo"></param>
        /// <returns></returns>
        public static TemplateDllPageInfo ExecutetemplateDll(TemplateDllPageInfo oTemplateDllPageInfo)
        {
            return ExecutetemplateDllByReadBytes(oTemplateDllPageInfo);
        }

        /// <summary>
        ///  根据指定的dll文件，执行模板解析
        /// </summary>
        /// <param name="oTemplateDllPageInfo"></param>
        /// <returns></returns>
        public static TemplateDllPageInfo ExecutetemplateDllByReadBytes(TemplateDllPageInfo oTemplateDllPageInfo)
        {
            Dictionary<string, Type> oDicItemplateControls = new Dictionary<string, Type>();
            ItemplateControl oItemplateControl = null;

            //从缓存中读取
            object objCache = mICache.Read("TemplateDlls_ItemplateControl");
            if (objCache != null && (objCache is Dictionary<string, Type>))
            {
                oDicItemplateControls = objCache as Dictionary<string, Type>;
            }
            else
            {
                mICache.Save("TemplateDlls_ItemplateControl", oDicItemplateControls, 60 * 30);//保存30分钟
            }

            //如果Dictionary不存在，则开始读取文件并将
            if (oDicItemplateControls.ContainsKey(oTemplateDllPageInfo.strTemplateDLLPath) == false)
            {
                if (!File.Exists(oTemplateDllPageInfo.strTemplateDLLPath))
                {
                    oTemplateDllPageInfo.strErrorInfo = "dll文件不存在";
                    return oTemplateDllPageInfo;
                }

                byte[] assemblyInfo = File.ReadAllBytes(oTemplateDllPageInfo.strTemplateDLLPath);
                Assembly assembly = Assembly.Load(assemblyInfo);

                foreach (Type oTempType in assembly.GetTypes())
                {
                    if (!oTempType.IsPublic || oTempType.IsAbstract || oTempType.IsInterface) continue;
                    Type concreteType = oTempType.GetInterface(typeof(ItemplateControl).FullName, true);
                    if (concreteType != null)
                    {
                        oItemplateControl = (ItemplateControl)Activator.CreateInstance(oTempType);

                        if (oDicItemplateControls.ContainsKey(oTemplateDllPageInfo.strTemplateDLLPath) == false)
                        {
                            oDicItemplateControls.Add(oTemplateDllPageInfo.strTemplateDLLPath, oTempType);
                        }

                        mICache.Save("TemplateDlls_ItemplateControl", oDicItemplateControls, 60 * 20);

                        break;
                    }
                }
            }
            else
            {
                DateTime dtStart = DateTime.Now;
                oItemplateControl = (ItemplateControl)Activator.CreateInstance(oDicItemplateControls[oTemplateDllPageInfo.strTemplateDLLPath]);
                DateTime dtEnd = DateTime.Now;
                oTemplateDllPageInfo.intDataTimeSpan = Convert.ToInt32(dtEnd.Subtract(dtStart).TotalMilliseconds);
            }


            if (oItemplateControl != null)
            {
                return oItemplateControl.Analysis(oTemplateDllPageInfo);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 从cache中删除某个可执行程序
        /// </summary>
        /// <param name="oTemplateDllPageInfo"></param>
        public static void ReMoveOneDllFromCache(TemplateDllPageInfo oTemplateDllPageInfo)
        {
            Dictionary<string, Type> oDicItemplateControls = new Dictionary<string, Type>();
            //从缓存中读取
            object objCache = mICache.Read("TemplateDlls_ItemplateControl");
            if (objCache != null && (objCache is Dictionary<string, Type>))
            {
                oDicItemplateControls = objCache as Dictionary<string, Type>;
                if (oDicItemplateControls.ContainsKey(oTemplateDllPageInfo.strTemplateDLLPath))
                {
                    oDicItemplateControls.Remove(oTemplateDllPageInfo.strTemplateDLLPath);
                    mICache.Save("TemplateDlls_ItemplateControl", oDicItemplateControls, 60 * 30);//保存30分钟
                }
            }
            else
            {
                mICache.Save("TemplateDlls_ItemplateControl", oDicItemplateControls, 60 * 30);//保存30分钟
                return;
            }
        }

    }
}
